﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SuCasa.DataAccess;
using SuCasa.Infrastructure;
using SuCasa.Models;

namespace SuCasa.Controllers
{
    [Authorize]
    public class MemberController : Controller
    {
        readonly IMemberRepository _memberRepository;

        public MemberController(){
            if (Constants.UseFakes){
                _memberRepository = new FakeMemberRepository();
            }
            else{
                _memberRepository = new MemberRepository(SuCasaContext.Instance);
            }
        }

        [HttpGet]
        public ActionResult Add(){
            ViewBag.Title = "Add";
            var viewModel = new MemberAddEntryViewModel
                {
                    Members = _memberRepository.All
                };
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Add(MemberAddEntryViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var member = new Member();
                AutoMapper.Mapper.Map(viewModel, member);
                if (!_memberRepository.AddMember(member))
                {
                    ModelState.AddModelError(string.Empty, "Couldn't persist member.");
                }
                if (Request.Files.Count > 0 && Request.Files[0] != null)
                {
                     HttpPostedFileBase file = Request.Files[0];
                     file.SaveAs("~/pictures/" + file.FileName);
                }
            }
            return RedirectToAction("Index", "Home");
        }

        [HttpGet]
        public ActionResult Edit(int id){
            var viewModel = new MemberAddEntryViewModel();
            var member = _memberRepository.GetMemberBy(x => x.MemberId == id);
            AutoMapper.Mapper.Map(member, viewModel);
            viewModel.Members = _memberRepository.All;
            ViewBag.Title = "Update";
            return View("Add", viewModel);
        }

        [HttpGet]
        public void Delete(int id){
            _memberRepository.Delete(id);
        }

        //[HttpPost]
        //public ActionResult Edit(MemberEntryViewModel viewModel){
        //    if (ModelState.IsValid){
                
        //    }
        //}

        [HttpGet]
        public ActionResult Search()
        {
            return View(new MemberSearchViewModel());
        }

        [HttpPost]
        public ActionResult Search(string searchStr){
            IEnumerable<Member> members = null;
            if (ModelState.IsValid){
                members = _memberRepository.GetMembers(searchStr);
            }

            return View("SearchResults", new MemberSearchViewModel(searchStr, members.ToList()));
            
        }

        [HttpGet]
        public ActionResult Details(int id){
            var viewModel = new MemberDetailsViewModel();
            if (ModelState.IsValid){
                var member = _memberRepository.GetMemberBy(x => x.MemberId == id);
                if (member == null){
                    ModelState.AddModelError(string.Empty, "Couldn't get member.");
                } else{
                    AutoMapper.Mapper.Map(member, viewModel);
                    viewModel.Children = _memberRepository.GetAll().ToList().Where(x => x.ParentMemberId == id);
                }
            }
            return View(viewModel);
        }
    }

   
}
